home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / c-runtime / dispatch / Object.m < prev    next >
Encoding:
Text File  |  1992-04-21  |  8.0 KB  |  379 lines

  1. /* -*-c-*- */
  2.  
  3. /* Copyright (C) 1989, 1992 Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* As a special exception, if you link this library with files
  22.    compiled with GCC to produce an executable, this does not cause
  23.    the resulting executable to be covered by the GNU General Public License.
  24.    This exception does not however invalidate any other reasons why
  25.    the executable file might be covered by the GNU General Public License.  */
  26.  
  27. /* 
  28.   $Header: /usr/user/dennis_glatting/ObjC/c-runtime/dispatch.common/RCS/Object.m,v 0.19 1992/04/18 01:11:50 dennisg Exp $
  29.   $Author: dennisg $
  30.   $Date: 1992/04/18 01:11:50 $
  31.   $Log: Object.m,v $
  32. # Revision 0.19  1992/04/18  01:11:50  dennisg
  33. # changes some name accesses to use functions.
  34. #
  35. # Revision 0.18  1992/04/18  00:43:06  dennisg
  36. # Changed -hash.
  37. # It returned 'self' casted to a unsigned int.
  38. #
  39. # Revision 0.17  1992/04/13  11:43:08  dennisg
  40. # Check in after array version of run-time works.
  41. # Expect more changes as hash version and other changes are made.
  42. #
  43. # Revision 0.16  1992/02/25  11:16:03  dennisg
  44. # ??? I lost track.
  45. #
  46. # Revision 0.15  1992/01/03  02:55:03  dennisg
  47. # modified to handle new initialization scheme.
  48. # fixed code structure.
  49. #
  50. # Revision 0.14  1991/12/31  20:13:03  dennisg
  51. # Deleted index variable stuff.  Index variables are a hack to the language.
  52. #
  53. # Revision 0.13  1991/12/10  12:04:55  dennisg
  54. # Cleaned up file format for a distribution.
  55. #
  56. # Revision 0.12  1991/12/07  00:49:28  dennisg
  57. # deleted +description:.
  58. #
  59. # Revision 0.11  1991/12/06  00:35:05  dennisg
  60. # deleted perform:with:: method.
  61. # changes the other perform: methods to use objc_msgSend() correctly.
  62. #
  63. # Revision 0.10  1991/11/30  14:19:18  dennisg
  64. # implemented archiving.
  65. #
  66. # Revision 0.9  1991/11/29  21:59:32  dennisg
  67. # modified to implement set functions.
  68. #
  69. # Revision 0.8  1991/11/29  20:01:29  dennisg
  70. # fixed several const decls.  bozo.
  71. #
  72. # Revision 0.7  1991/11/29  14:09:51  dennisg
  73. # changed some methods to implement functions contained in the core code.
  74. #
  75. # Revision 0.6  1991/11/26  02:45:16  dennisg
  76. # commiting a copy of the source to rcs before i implement
  77. # changes to fix posing.
  78. #
  79. # Revision 0.5  1991/11/20  02:06:30  dennisg
  80. # returned doesNotRecognize:
  81. #
  82. # Revision 0.4  1991/11/19  12:37:24  dennisg
  83. # minr changes.  still in a state of flux.
  84. #
  85. # Revision 0.3  1991/11/16  13:32:58  dennisg
  86. # changed #import statements to #include.
  87. # this should make gcc2 happy -- geez.
  88. #
  89. # Revision 0.2  1991/11/07  22:30:54  dennisg
  90. # added copyleft
  91. #
  92. # Revision 0.1  1991/10/24  00:45:39  dennisg
  93. # Initial check in.  Preliminary development stage.
  94. #
  95. */
  96.  
  97.  
  98. #include <Object.h>
  99. #include <objc-proto.h>
  100. #include <objc-protoP.h>
  101.  
  102. #include <errno.h>
  103. #include <stdarg.h>
  104. #include <stdlib.h>
  105.  
  106.  
  107. #define CLASS( class )  (( Class_t )class )
  108.  
  109.  
  110. @implementation Object
  111.  
  112.  
  113. + new {
  114.  
  115.  
  116.   return class_createInstance( CLASS( self ));
  117. }
  118.  
  119.  
  120. + free {  return nil; }
  121. - free {  return object_dispose( self ); }
  122.  
  123.  
  124. - copy {  return [ self shallowCopy ]; }
  125.  
  126.  
  127. - shallowCopy {
  128.  
  129.  
  130.   return object_copy ( self );
  131. }
  132.  
  133.  
  134. - deepCopy {
  135.  
  136.   
  137.   return class_createInstance ([ self class ]);
  138. }
  139.  
  140.  
  141. + ( Class_t )class      { return CLASS( self ); }
  142. + ( Class_t )superClass { return CLASS( self )->super_class; }
  143. - ( Class_t )class      { return isa; }
  144. - ( Class_t )superClass { return isa->super_class; }
  145. - ( const char * )name  { return object_getClassName (self); }
  146. - self                  { return self; }
  147.  
  148.  
  149. - ( u_int )hash {
  150.  
  151.   
  152.   return ( u_int )self; /* gak!  Not portable. */
  153. }
  154.  
  155.  
  156. - ( BOOL )isEqual:anObject {
  157.  
  158.   
  159.   return self == anObject ;
  160. }
  161.  
  162.  
  163. - ( BOOL )isKindOf:( Class_t )aClassObject {
  164.  
  165.  
  166.   return [ self isKindOfGivenName:class_getClassName (aClassObject)];
  167. }
  168.  
  169.  
  170. - ( BOOL )isMemberOf:( Class_t )aClassObject {
  171.  
  172.  
  173.   return isa == aClassObject ;
  174. }
  175.  
  176.  
  177. - ( BOOL )isKindOfGivenName:( const char* )aClassName {
  178.  
  179.   Class_t   class;
  180.   
  181.   
  182.   for( class = isa; class; class = class->super_class )
  183.     if( !strcmp( class_getClassName (class), aClassName ))
  184.       return YES;
  185.   
  186.   return NO;
  187. }
  188.  
  189.  
  190. - ( BOOL )isMemberOfGivenName:( const char* )aClassName {
  191.  
  192.  
  193.   return !strcmp([ self name ], aClassName );
  194. }
  195.  
  196.  
  197. + ( BOOL )instancesRespondTo:( SEL )aSel {
  198.  
  199.   
  200.   if( class_getInstanceMethod( CLASS( self ), aSel ))
  201.     return YES;
  202.   
  203.   return NO;
  204. }
  205.  
  206.  
  207. - ( BOOL )respondsTo:( SEL )aSel {
  208.  
  209.  
  210.   if( class_getInstanceMethod( isa, aSel ))
  211.     return YES;
  212.  
  213.   return NO;
  214. }
  215.  
  216.  
  217. - perform:( SEL )aSel {
  218.  
  219.   
  220.   return (*((IMP)objc_msgSend( self, aSel )))(self, aSel);
  221. }
  222.  
  223.  
  224. - perform:( SEL )aSel with:aObject {
  225.  
  226.   
  227.   return (*((IMP)objc_msgSend( self, aSel )))(self, aSel, aObject);
  228. }
  229.  
  230.  
  231. + poseAs:( Class_t )aClassObject {
  232.  
  233.  
  234.   return class_poseAs (self, aClassObject);
  235. }
  236.  
  237.  
  238. - subclassResponsibility:( SEL )aSel {
  239.  
  240.   
  241.   return [ self error:"subclass should override %s", aSel ];
  242. }
  243.  
  244.  
  245. - notImplemented:(SEL)aSel {
  246.  
  247.  
  248.   return [ self error:"method %s not implemented", aSel ];
  249. }
  250.  
  251.  
  252. - doesNotRecognize:( SEL )aSel {
  253.  
  254.  
  255.   return [ self error:"%s does not recognize %s", [ self name ], aSel ];
  256. }
  257.  
  258. - error:( const char* )aString, ... {
  259.  
  260. #define FMT "error: %s (instance)\n%s\n"
  261.  
  262.   char  fmt[ strlen (FMT) + 
  263.               strlen (isa->name) +
  264.               strlen (aString) + 8 ];
  265.   va_list ap;
  266.   
  267.   
  268.   sprintf (fmt, FMT, isa->name, aString);
  269.   va_start (ap, aString);
  270.   (*_error)(self, fmt, ap);
  271.   va_end (ap);
  272.  
  273. #undef FMT
  274.   return self;
  275. }
  276.  
  277.  
  278. + error:( const char* )aString, ... {
  279.  
  280. #define FMT "error: %s (class)\n%s\n"
  281.  
  282.   char  fmt[ strlen (FMT) + 
  283.               strlen (isa->name) +
  284.               strlen (aString) + 8 ];
  285.   va_list ap;
  286.   
  287.   
  288.   sprintf (fmt, FMT, isa->name, aString);
  289.   va_start (ap, aString);
  290.   (*_error)(self, fmt, ap);
  291.   va_end (ap);
  292.  
  293. #undef FMT
  294.   return self;
  295. }
  296.  
  297.  
  298. - storeOn:( int )aFd {
  299.  
  300.   int   len;
  301.   long  version = [[ self class ] version ];
  302.   
  303.   
  304.   if ((len = write (aFd, "#", strlen ("#"))) != -1)
  305.     if ((len = write (aFd, [ self name ], strlen ([ self name ]) + 1)) != -1)
  306.       len = write (aFd, &version, sizeof (version));
  307.   
  308.   if (len == -1)
  309.     [ self error:"error passivating object, errno=%d", errno ];
  310.  
  311.   return self;
  312. }
  313.  
  314.  
  315. + readFrom:( int )aFd {
  316.  
  317.   id    aObj = nil;
  318.   char  objName[256];
  319.   int   len;
  320.   
  321.   
  322.   if ((len = read (aFd, &objName, strlen ("#"))) != -1)
  323.     if (objName[0] == '#') {
  324.       long  version;
  325.       int   i = 0;
  326.       
  327.                                                 /* Read the object's class. */
  328.       do {
  329.         len = read (aFd, &objName[i], sizeof (char));
  330.       } while (objName[ i++ ] && (len != -1));
  331.     
  332.                                                 /* Get its version. */
  333.       if (len != -1)
  334.         len = read (aFd, &version, sizeof (version));
  335.       
  336.                                                 /* No errors???
  337.                                                   Then create a object. */
  338.       if (len != -1) {
  339.           aObj = class_createInstance (objc_getClass (objName));
  340.           
  341.                                                 /* If the object was 
  342.                                                   successfully created then
  343.                                                   tell it to dearchive
  344.                                                   itself. */
  345.           if (aObj)
  346.             [ aObj readFrom:aFd ];
  347.       }
  348.     }
  349.     
  350.   if (len == -1)
  351.     [ self error:"error activating object, errno=%d", errno ];
  352.   
  353.   return aObj;
  354. }
  355.  
  356.  
  357. - readFrom:( int )aFd { return self; }
  358.  
  359.  
  360. + ( int )version {
  361.  
  362.  
  363.   return class_getVersion (CLASS (self));
  364. }
  365.  
  366.  
  367. + setVersion:( int )aVersion {
  368.  
  369.  
  370.   class_setVersion (CLASS (self), aVersion);
  371.   
  372.   return self;
  373. }
  374.  
  375.  
  376. @end
  377.  
  378.  
  379.